/*
 * Created on Sep 3, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.herohead;


import java.io.File;
import java.util.Date;

import com.stevesoft.pat.Regex;

/**
 * @author david-1
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class FileIO {

	public static long fileCount = 0;
	public static long dirCount = 0;

	private static long getFolder(File directory) {
		long length = 0;

		for (File file : directory.listFiles()) {
			if (file.isFile()) {
				length += file.length();
				fileCount += 1;
			} else {
				dirCount += 1;
				length += getFolder(file);
			}
		}
		return length;

	}

	private long totalDriveSize = 0;


	private long spaceDriveFree = 0;
	/**
	 * checks if the directory exists.
	 * @param directory
	 * @return true if exist or false is not found.
	 */
	public boolean checkDirectory (String directory) {

		File dir = new File(directory);

		if (!dir.canRead()) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * Convert Directory slashes
	 * @param str
	 * @return converted directory path
	 */
	public String convertDirectory(String str) {

		Regex r = new Regex("\\","/");

		return r.replaceAll(str);
	}

	public void diskSpaceDetail(String driveLetter)	{

		File file = new File(driveLetter);

		totalDriveSize = file.getTotalSpace(); //total disk space in bytes.
		//usableDriveFree = file.getUsableSpace(); ///unallocated / free disk space in bytes.
		spaceDriveFree = file.getFreeSpace(); //unallocated / free disk space in bytes.

		//System.out.println(" === Partition Detail ===");

		//System.out.println(" === bytes ===");
		//System.out.println("Total size : " + totalSpace + " bytes");
		//	System.out.println("Usable free : " + usableSpace + " bytes");
		//	System.out.println("Space free : " + freeSpace + " bytes");

		//	System.out.println(" === mega bytes ===");
		//	System.out.println("Total size : " + totalSpace /1024 /1024 + " mb");
		//	System.out.println("Usable free : " + usableSpace /1024 /1024 + " mb");
		//	System.out.println("Space free : " + freeSpace /1024 /1024 + " mb");
	}

	public String fileTimeStampString (File fle) {

		long t;

		//t = System.currentTimeMillis();
		t = fle.lastModified();
		String rightNow = "" + new Date(t);
		return rightNow;
	}



	public long getDirCount() {
		return dirCount;
	}
	public long getFileCount() {
		return fileCount;
	}




	public  long getFolderSize(String dir) {
		final File folder = new File(dir);
		return getFolder(folder);
	}

	public long getSpaceDriveFree() {
		return spaceDriveFree;
	}

	public long getSpaceDriveFreeMB() {
		return spaceDriveFree  / 1024 /1024;
	}

	public long getTotalDriveSize() {
		return totalDriveSize;
	}

	public long getTotalDriveSizeMB() {
		return totalDriveSize / 1024 /1024 ;
	}

}

